#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1005;
const int M = 1e5 + 5;

int n, m;
int f[N];
struct Edge {
  int u, v, w;
  bool operator<(Edge e) const { return w < e.w; }
} edges[M];

void init() { rep(i, 1, n) f[i] = i; }
int find(int x) { return f[x] == x ? x : find(f[x]); }
bool merge(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx == fy) return false;
  f[fy] = fx;
  return true;
}
int kruskal() {
  if (n == 1) return 0;
  init();
  sort(edges + 1, edges + m + 1);
  int cnt = 0;
  rep(i, 1, m) {
    if (merge(edges[i].u, edges[i].v)) {
      if (++cnt == n - 1) return edges[i].w;
    }
  }
  return -1;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, m) cin >> edges[i].u >> edges[i].v >> edges[i].w;
  cout << kruskal();
  return 0;
}